Storage.prototype.setExpire = (key, value, expire) => {
    let obj = {
        data: value,
        time: Date.now(),
        expire: expire
    };
    //localStorage 设置的值不能为对象,转为json字符串
    localStorage.setItem(key, JSON.stringify(obj));
}

Storage.prototype.getExpire = key => {
    let val = localStorage.getItem(key);
    if (!val) {
        return val;
    }
    val = JSON.parse(val);
    if (Date.now() - val.time > val.expire) {
        localStorage.removeItem(key);
        return null;
    }
    return val.data;
}


layui.use(['layer', 'element', 'admin', 'index', 'laytpl', 'element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var admin = layui.admin;
    var index = layui.index;
    var laytpl = layui.laytpl;
    var element = layui.element;


    var Authorization ;

    init();

    function init() {
        // var params = window.location.search;
        // if(params!=""&&params!=null){
        //     // var string_Authorization = decodeURI(params);
        //     // Authorization = string_Authorization.substr(1);
        //     // var Authorization_parse = JSON.parse(Authorization);
        //     localStorage.setExpire('token_type','bearer',sysTimeOut);
        //     localStorage.setExpire('access_token',params.split('?')[1],sysTimeOut);
        //     getUserInfo();
        // }else{
        //     userInfo = getAuthority();
        //     if(userInfo){
        //         initUserInfo(userInfo);
        //         initMenuTree(userInfo);
        //     }
        // }

        userInfo = getAuthority();
        if(userInfo){
            initUserInfo(userInfo);
            initMenuTree(userInfo);
        }
    }

    //初始化用户信息
    function initUserInfo(userInfo) {
        let arr = JSON.parse(userInfo);
        $(".userInfoBox cite").text(arr.sysUser.username)
    }

    //初始化左边菜单
    function initMenuTree(userInfo) {
        getMenuTree(function () {//每次页面刷新重新获取左边菜单树
            // ajax渲染侧边栏
            var menuTree = JSON.parse(localStorage.getExpire("menuTree"));
            laytpl(sideNav.innerHTML).render(menuTree, function (html) {
                $('*[lay-filter=admin-side-nav]').html(html);
                element.render('nav');
                admin.activeNav('page/metadata/tableClassify.html');
            });
        });

        // 默认加载主页
        index.loadHome({
            menuPath: 'page/metadata/tableClassify.html',
            menuName: '<i class="layui-icon layui-icon-home"></i>'
        });
    }

    // 获取左边树
    function getMenuTree(callback) {
        var access_token = localStorage.getExpire("access_token");
        var token_type = localStorage.getExpire("token_type");
        var Authorization = token_type+" "+access_token;
        let req = {
            system_identifier:'system_data'
        }
        axios({
            method: 'get',
            url:loginUrl.menuTree,
            params:req,
            headers:{
                'Authorization':Authorization}
        })
            .then(function (response) {
                if(response.status == 200){
                    var data = response.data;
                    localStorage.setExpire("menuTree",JSON.stringify(data.data),sysTimeOut)
                    callback();
                }
            })
            .catch(function (error) {
                console.log(error);
            });
    }



// 获取用户信息
function getUserInfo() {
    var access_token = localStorage.getExpire("access_token");
    console.log('access_token:',access_token)
    axios.get(loginUrl.userInfo,{
        params: {
            access_token: access_token
        }
    })
    .then(function (response) {
        if(response.status == 200 &&response.data.message == "success"){
            var data = JSON.stringify(response.data.data);
            localStorage.setExpire("userInfo",JSON.stringify(response.data.data),sysTimeOut)
            initUserInfo(data);
            initMenuTree(data);
        }
    })
    .catch(function (error) {
        console.log(error);
    });
}

})
